package com.nmgs.util.zip;

import org.apache.commons.compress.archivers.ArchiveException;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.compress.archivers.jar.JarArchiveEntry;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.filechooser.FileNameExtensionFilter;
import java.io.*;

public class WarUtil {
    private static final Logger logger = LoggerFactory.getLogger(WarUtil.class);
    /**
     * war文件格式的过滤器
     */
    private static FileNameExtensionFilter filter = new FileNameExtensionFilter(
            "WAR压缩文件(*.war)", "war");
    /**
     * 解压缩war包
     * @param filePath:war包所在路径，包含本身文件名
     * @param destPath:解压缩到路径
     * */
    public static void unCompressWar(String filePath, String destPath) throws IOException, ArchiveException {
        unCompressWar(new FileInputStream(filePath),destPath);
    }

    /**
     * 解压缩war包
     * @param srcFile:war包文件
     * @param destPath:解压缩到路径
     * */
    public static void unCompressWar(File srcFile, String destPath) throws IOException, ArchiveException {
        unCompressWar(new FileInputStream(srcFile),destPath);
    }

    /**
     * 解压缩war包
     * @param warInputStream:war包输入流
     * @param destPath:解压缩到路径
     * */
    public static void unCompressWar(InputStream warInputStream, String destPath) throws IOException, ArchiveException {
        ArchiveInputStream in = null;
        try {
            BufferedInputStream bufferedInputStream = new BufferedInputStream(warInputStream);
            in = new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.JAR, bufferedInputStream);
            JarArchiveEntry entry = null;
            logger.info("解压文件: 到 {}", destPath);
            while ((entry = (JarArchiveEntry) in.getNextEntry()) != null) {
                if (entry.isDirectory()) {
                    File newFile = UtilFile.newFileWithCheck(destPath,  entry.getName());
                    if(!newFile.exists()){
                        logger.info("文件夹{}不存在,创建.",newFile.getAbsolutePath());
                        newFile.mkdir();
                    }
                    if(newFile.exists() && !newFile.isDirectory()){
                        logger.info("不能解压文件{}到{},此文件己存在",entry.getName(),newFile.getAbsolutePath());
                    }
                } else {
                    File outFile = UtilFile.newFileWithCheck(destPath,  entry.getName());
                    if(!outFile.getParentFile().exists()){
                        logger.info("文件夹{}不存在,创建.",outFile.getParentFile().getAbsolutePath());
                        outFile.getParentFile().mkdirs();
                    }
                    logger.info("解压{} 至 {}",entry.getName(),outFile.getAbsolutePath());
                    OutputStream out = FileUtils.openOutputStream(outFile);
                    IOUtils.copy(in, out);
                    out.close();
                }
            }
        } catch (FileNotFoundException e) {
            logger.error("未找到war文件");
            throw new FileNotFoundException("未找到war文件");
        } catch (ArchiveException e) {
            logger.error("不支持的压缩格式");
            throw new ArchiveException("不支持的压缩格式");
        } catch (IOException e) {
            logger.error("文件写入发生错误");
            throw new IOException("文件写入发生错误");
        }finally{
            in.close();
            logger.info("解压结束.");
        }
    }
    public FileNameExtensionFilter getFileFilter() {
        return filter;
    }
}
